package io.itit.ecp.admin.provide.filter;

import com.alibaba.fastjson.serializer.ValueFilter;
import io.itit.grass.common.server.helper.DictHelper;
import io.itit.grass.common.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：数据范围键值转换
 * @Date：Create by 2024-01-17 16:55:06
 */
public interface IDataRangeFilter {

	String RANGE_LEVEL = "rangeLevel" ;
	String STATUS = "status" ;

	String DATA_RANGE_RANGE_LEVEL = "ecp-admin.dataRange.rangeLevel" ;
	String DATA_RANGE_STATUS = "ecp-admin.dataRange.status" ;

	class DataRangeExportFilter implements ValueFilter {

		private Map<String, String> rangeLevelMap;
		private Map<Integer, String> statusMap;

		public DataRangeExportFilter() {
			rangeLevelMap = DictHelper.i().stringVlMap(DATA_RANGE_RANGE_LEVEL);
			statusMap = DictHelper.i().integerVlMap(DATA_RANGE_STATUS);
		}

		@Override
		public Object process(Object object, String name, Object value) {
			if (StringUtils.equals(name, RANGE_LEVEL)) {
				return CommonUtils.orElse(rangeLevelMap.get(CommonUtils.toString(value)), value);
			}
			if (StringUtils.equals(name, STATUS)) {
				return CommonUtils.orElse(statusMap.get(CommonUtils.toInteger(value)), value);
			}
			return value;
		}

	}

	class DataRangeUploadFilter implements ValueFilter {

		private Map<String, String> rangeLevelMap;
		private Map<String, Integer> statusMap;

		public DataRangeUploadFilter() {
			rangeLevelMap = DictHelper.i().stringLvMap(DATA_RANGE_RANGE_LEVEL);
			statusMap = DictHelper.i().integerLvMap(DATA_RANGE_STATUS);
		}

		@Override
		public Object process(Object object, String name, Object value) {
			if (StringUtils.equals(name, RANGE_LEVEL)) {
				return CommonUtils.orElse(rangeLevelMap.get(CommonUtils.toString(value)), value);
			}
			if (StringUtils.equals(name, STATUS)) {
				return CommonUtils.orElse(statusMap.get(CommonUtils.toString(value)), value);
			}
			return value;
		}

	}

}